﻿using System;
using System.IO;
using System.Windows.Forms;
using iTunesUtil.Properties;

namespace iTunesUtil
{
    public partial class FilterLibraryFile : Form
    {
        readonly Settings set = Settings.Default;   
        public FilterLibraryFile()
        {
            InitializeComponent();

            textBoxFilterIn.Text = set.FilterFileIn;
            textBoxFilterOut.Text = set.FilterFileOut;
            textBoxInFile.Text = set.FilterFileLocationIn;
            textBoxOutFile.Text = set.FilterFileLocationOut;
            textBoxMusicFolderIn.Text = set.FilterMusicFolderIn;
            textBoxMusicFolderOut.Text = set.FilterMusicFolderOut;            
        }

        private void buttonBrowseInFile_Click(object sender, EventArgs e)
        {
            textBoxInFile.Text = BrowseForFile(textBoxInFile.Text);
        }

        private void buttonBrowseOutFile_Click(object sender, EventArgs e)
        {
            textBoxOutFile.Text = BrowseForFile(textBoxOutFile.Text);
        }
        void Save()
        {
            set.FilterFileIn = textBoxFilterIn.Text;
            set.FilterFileOut = textBoxFilterOut.Text;
            
            set.FilterFileLocationIn = textBoxInFile.Text;
            set.FilterFileLocationOut = textBoxOutFile.Text;

            set.FilterMusicFolderIn = textBoxMusicFolderIn.Text;
            set.FilterMusicFolderOut = textBoxMusicFolderOut.Text;
            set.Save();
        }
        void Replace()
        {
            StreamWriter writer = new StreamWriter(textBoxOutFile.Text);
            StreamReader reader = new StreamReader(textBoxInFile.Text);
            string oldValue = textBoxFilterIn.Text;
            string newValue = textBoxFilterOut.Text;
            string musicFolderOld = textBoxMusicFolderIn.Text;
            string musicFolderNew = textBoxMusicFolderOut.Text;            

            while (!reader.EndOfStream)
            {
                writer.WriteLine(reader.ReadLine().Replace(musicFolderOld, musicFolderNew).Replace(oldValue, newValue));
            }
            writer.Close();
            reader.Close();            
        }
        private void buttonReplace_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                Save();
                Replace();
            }
            catch(Exception ex)
            {
                 MessageBox.Show(ex.ToString(), "Error !", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }


        string BrowseForFile(string fileName)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "All files (*.*)|*.*";
            fileDialog.FilterIndex = 0;
            fileDialog.RestoreDirectory = true;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                return fileDialog.FileName;
            }
            return fileName;
        }        
    }
}
